package com.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.domain.dto.SetmealDto;
import com.example.domain.entity.Setmeal;
import com.example.domain.entity.SetmealDish;
import com.example.exception.RegisterException;
import com.example.mapper.SetmealMapper;
import com.example.service.ISetmealDishService;
import com.example.service.ISetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 套餐 服务实现类
 * </p>
 *
 * @author xiaoduan
 * @since 2024-07-30
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements ISetmealService {

    @Autowired
    private ISetmealDishService setmealDishService;

    @Override
    public void saveSetmeal(SetmealDto setmealDto) {
        save(setmealDto);
        // LambdaQueryWrapper<SetmealDish> wrapper = new LambdaQueryWrapper<>();
        // wrapper.eq(SetmealDish::getSetmealId, setmealDto.getId());
        List<SetmealDish> collect = setmealDto.getSetmealDishes().stream().map((item) -> {
            item.setSetmealId(String.valueOf(setmealDto.getId()));
            return item;
        }).collect(Collectors.toList());
        setmealDishService.saveBatch(collect);
    }

    @Override
    public void removeByIdsAndSetMealDish(String ids) {
        String[] split = ids.split(",");
        for (String s : split) {
            Setmeal byId = getById(s);
            if (byId.getStatus() == 1) {
                throw new RegisterException("套餐正在售卖中，不能删除",601);
            }
        }
        removeByIds(Arrays.asList(ids.split(",")));
        LambdaQueryWrapper<SetmealDish> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(SetmealDish::getSetmealId, ids.split(","));
        setmealDishService.remove(wrapper);
    }

    @Override
    public void piLiangQiShouTingShou(Integer status, String ids) {
        for (String s : ids.split(",")) {
            Setmeal byId = getById(s);
            byId.setStatus(status);
            updateById(byId);
        }
    }

    @Override
    public SetmealDto getStemealById(Long id) {
        Setmeal byId = getById(id);
        SetmealDto setmealDto = new SetmealDto();
        BeanUtils.copyProperties(byId,setmealDto);
        List<SetmealDish> list = setmealDishService.list(new LambdaQueryWrapper<SetmealDish>().eq(SetmealDish::getSetmealId, id));
        setmealDto.setSetmealDishes(list);
        return setmealDto;
    }

    @Override
    public void updateSetmeal(SetmealDto setmealDto) {
        updateById(setmealDto);
        setmealDishService.remove(new LambdaQueryWrapper<SetmealDish>().eq(SetmealDish::getSetmealId, setmealDto.getId()));
        List<SetmealDish> collect = setmealDto.getSetmealDishes().stream().map((item) -> {
            item.setSetmealId(String.valueOf(setmealDto.getId()));
            return item;
        }).collect(Collectors.toList());
        setmealDishService.saveBatch(collect);
    }

}
